package com.example.lvsuo.controller;


import com.example.lvsuo.entity.AdmmClientUser;
import com.example.lvsuo.entity.Client;
import com.example.lvsuo.entity.LogJob;
import com.example.lvsuo.entity.vo.AdmmClientUserVo;
import com.example.lvsuo.entity.vo.LogJobVo;
import com.example.lvsuo.modal.Result;
import com.example.lvsuo.service.IAdmmClientUserService;
import com.example.lvsuo.service.IClientService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * <p>
 * 客户/我的客户 前端控制器
 * </p>
 *
 * @author 心心念念
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/admmClientUser")
public class AdmmClientUserController {

    @Autowired
    IAdmmClientUserService iAdmmClientUserService;

    @Autowired
    IClientService iClientService;


    /**
     * 查询全部客户
     * @param admmClientUserVo
     * @return
     */
    @GetMapping(params = "chaxun=1")
    public Result getAdmmClientUser(AdmmClientUserVo admmClientUserVo){
        PageHelper.startPage(admmClientUserVo.getPageId(),admmClientUserVo.getPageNum());
        PageInfo pageInfo = new PageInfo(iAdmmClientUserService.getAdmmClientUser(admmClientUserVo));
        return Result.back("ok","200",pageInfo);
    }

    /**
     * 根据查询我的客户
     * @param admmClientUserVo
     * @return
     */
    @GetMapping(params = "chaxun=2")
    public Result getAdmmClientUserNameId(AdmmClientUserVo admmClientUserVo){
        List<AdmmClientUserVo> list=iAdmmClientUserService.getAdmmClientUserNameId(admmClientUserVo);
        return Result.back("ok","200",list);
    }

    /**
     * 查询我的客户
     * @param admmClientUserVo
     * @return
     */
    @GetMapping
    public Result getAdmmClientUserName(AdmmClientUserVo admmClientUserVo){
        PageHelper.startPage(admmClientUserVo.getPageId(),admmClientUserVo.getPageNum());
        PageInfo pageInfo = new PageInfo(iAdmmClientUserService.getAdmmClientUserName(admmClientUserVo));
        return Result.back("ok","200",pageInfo);
    }

    /**
     * 根据客户id查询
     * @param logJobVo
     * @return
     */
    @GetMapping("/{id}")
    public Result getAdmmClientUserId(LogJobVo logJobVo){
       List<LogJobVo> list=iAdmmClientUserService.getAdmmClientUserId(logJobVo);

        return Result.back("ok","200",new ArrayList(new LinkedHashSet(list)));
    }

    /**
     * 添加
     * @param client
     * @return
     */
    @PostMapping
    public Result addAdmmClientUser(@RequestBody Client client){
        iClientService.save(client); // 添加客户表
        // 查询新添加的客户id
        String clid = iClientService.queryId();

        AdmmClientUser admmClientUser = new AdmmClientUser();
        admmClientUser.setSystemUserId(client.getSystemUserId());
        admmClientUser.setClientId(Integer.valueOf(clid));
        admmClientUser.setClientUserCellPhoneNumber(client.getPhone());

        System.out.println("客户添加参数:"+client);
        System.out.println("参数为"+admmClientUser);
        iAdmmClientUserService.AdmmClientUserSave(admmClientUser); // 添加指派表

        return Result.back("ok","200");
    }

    /**
     * 修改
     * @param client
     * @return
     */
    @PutMapping
    public Result updateAdmmClientUser(@RequestBody Client client){
        System.out.println(client);
        iClientService.updateById(client);
        return Result.back("ok","200");
    }

}

